/**
 * Copyright 2024 Inc.
 **/
package com.myz.java.study.juc;

import lombok.Data;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @author maoyz0621 on 2024/4/25
 * @version v1.0
 */
@Data
public class DelayContext implements Delayed {

    private String context;

    private long availableTime;

    private long recordTime;


    public DelayContext(String context, long availableTime, long recordTime) {
        this.context = context;
        this.recordTime = recordTime;
        this.availableTime = availableTime + recordTime;
    }

    @Override
    public long getDelay(TimeUnit unit) {
        long diffTime = this.availableTime - System.currentTimeMillis();
        return unit.convert(diffTime, TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed o) {
        return (int) (this.availableTime - ((DelayContext) o).getAvailableTime());
    }
}